/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software. 
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package com.ecarx.systemui.plugin.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

/**
 * Created by ecarx on 18-9-6.
 */

public class LogUtils {
    private static final boolean DEBUG = true;
    private static String TAG = "XCSystemUIPlugin";

    public static void initTag(Context context,String strPackage){
        TAG = "XCSystemUIPlugin["+getVersionName(context,strPackage)+"]";
    }

    public static String getVersionName(Context context,String packageName) {
        PackageManager manager = context.getPackageManager();
        String strVerName = "";
        try {
            PackageInfo info = manager.getPackageInfo(packageName, 0);
            strVerName = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return strVerName;
    }


    public static void d(String msg) {
        if (DEBUG)
            Log.d(TAG, msg);
    }

    public static void w(String msg) {
        if (DEBUG) Log.w(TAG, msg);
    }

    public static void i(String msg) {
        if (DEBUG) Log.i(TAG, msg);
    }

    public static void e( String msg) {
        if (DEBUG) Log.e(TAG,  msg);
    }

    public static void d(String tag, String msg) {
        if (DEBUG) Log.i(TAG, tag + " : " + msg);
    }

    public static void w(String tag, String msg) {
        if (DEBUG) Log.w(TAG, tag + " : " + msg);
    }

    public static void i(String tag, String msg) {
        if (DEBUG) Log.i(TAG, tag + " : " + msg);
    }

    public static void e(String tag, String msg, Throwable e) {
        if (DEBUG) Log.e(TAG, tag + " : " + msg, e);
    }

    public static void e(String tag, String msg) {
        if (DEBUG) Log.e(TAG, tag + " : " + msg);
    }


    /**
     * author  : chong.huang
     * time    : 3/23/21 2:17 PM
     * desc    : 自动堆栈推断TAG, 并且可导航点击
     */

    public static void stack(Object... obj) {
        if (!DEBUG) return;
        Thread thread = Thread.currentThread();
        StackTraceElement stackTraceElement = thread.getStackTrace()[3];

        String fileName = stackTraceElement.getFileName();
        int lineNumber = stackTraceElement.getLineNumber();
        String methodName = stackTraceElement.getMethodName();

        StringBuilder builder = new StringBuilder();
        builder.append("(")
                .append(fileName)
                .append(":")
                .append(lineNumber)
                .append(")")
                .append("  ")
                .append(methodName)
                .append(": ");
        for (Object o : obj) {
            builder.append(o == null ? "null" : o.toString());
            builder.append("    ");
        }
        Log.e("systemuiplugin", builder.toString());
    }

    public static void stackTag(String tag, Object... obj) {
        if (!DEBUG) return;
        Thread thread = Thread.currentThread();
        StackTraceElement stackTraceElement = thread.getStackTrace()[3];

        String fileName = stackTraceElement.getFileName();
        int lineNumber = stackTraceElement.getLineNumber();
        String methodName = stackTraceElement.getMethodName();

        StringBuilder builder = new StringBuilder();
        builder.append("(")
                .append(fileName)
                .append(":")
                .append(lineNumber)
                .append(")")
                .append("  ")
                .append(methodName)
                .append(": ");
        for (Object o : obj) {
            builder.append(o == null ? "null" : o.toString());
            builder.append("    ");
        }
        Log.e(tag, builder.toString());
    }

    public static void stackE(String tag, Throwable e, Object... obj) {
        if (!DEBUG) return;
        Thread thread = Thread.currentThread();
        StackTraceElement stackTraceElement = thread.getStackTrace()[3];

        String fileName = stackTraceElement.getFileName();
        int lineNumber = stackTraceElement.getLineNumber();
        String methodName = stackTraceElement.getMethodName();

        StringBuilder builder = new StringBuilder();
        builder.append("(")
                .append(fileName)
                .append(":")
                .append(lineNumber)
                .append(")")
                .append("  ")
                .append(methodName)
                .append(": ");
        for (Object o : obj) {
            builder.append(o == null ? "null" : o.toString());
            builder.append("    ");
        }
        Log.e(tag, builder.toString(), e);
    }


}
